﻿using fuzhu;
using SqlSugar;
using System.Data;
using System.Numerics;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using ZDEntity;

namespace Yonyou.U8.BO
{
    /// <summary>
    ///不能直接继承 
    /// </summary>
    public class VendorVM
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string xmlConfig { get; set; }
        public static string cDatabase = "";

        public AS_Vendor entity { get; set; }

        public VendorVM()
        {
            //conStr
            conStr = CanShu.conStr;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }

        /// <summary>
        /// 导入U8，新增或修改
        /// </summary>
        /// <returns></returns>
        public RetModel2 Import()
        {

            try
            {
                //检查是否存在这个编码
                bool binv = Exists(entity.cVenCode);
                
                if (!binv)
                {
                    return this.Add();
                }
                else
                {

                    //加上供应商发展日期
                
                    return this.Update();
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }



        }
        public RetModel2 Add()
        {
            try
            {
                string cerr;
                string cid;
                 //末级编码系统自动处理了
                if (VendorDALCO.AddVendor(this.entity, xmlConfig, out cerr, out cid))
                {
                     
                    return DBService.ReOKObject("新增完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        /// <summary>
        ///  变更
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public RetModel2  Update()
        {
            try
            {
                string cerr;
                string cid; 
                if (VendorDALCO.ModifyVendor(this.entity, xmlConfig, out cerr))
                {
                     
                    return DBService.ReOKObject("修改完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
             
        }

        public RetModel2 Delete()
        {
            try
            {
                string cerr;
                string cid;

                if (VendorDALCO.DelVendor(entity.cVenCode, xmlConfig, out cerr))
                {
                    return DBService.ReOKObject("删除完成");
                }
                else
                {
                    return DBService.ReErrObject(cerr);
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }
        /// <summary>
        /// 检查供应商是否存在，返回U8供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Vendor GetByCode()
        {
            if (entity.cVenCode == null)
            {
                return null;
            }
            
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            { 
                
                Vendor vendor = db.Queryable<Vendor>().Where(x => x.cVenCode == entity.cVenCode).First();
                 return vendor; 
            } 
        
        }
        /// <summary>
        /// 检查供应商是否存在
        /// </summary>
        /// <param name="cVenCode"></param>
        /// <returns></returns>
        public bool Exists(string cVenCode)
        {
           
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = CanShu.conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {


              var cus = db.Queryable<Vendor>().Where(x => x.cVenCode == cVenCode).First();
                if (cus==null)
                {
                    return false;
                }
                else
                { //如果存在，加上发展日期
                    this.entity.dVenDevDate  =  DbHelper.GetDbDate(cus.dVenDevDate).ToString("yyyy-MM-dd");
                    return true;
                }
             


             
            }
        }

        
        public void MergeEntity()
        {
            //取默认值合并
            entity = ASService.MergeEntity<AS_Vendor>(entity, CanShu.venModel);
        }
    }
}
